package com.clgc.eduservice.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @author lsr
 * @create 2021-03-19-21:03
 */
@Configuration
@MapperScan("com.clgc.eduservice.mapper")
public class EduServiceConfig {

    //配置逻辑删除的插件
    @Bean
    public ISqlInjector iSqlInjector() {

        return new LogicSqlInjector();
    }

    //分页查询的组件
    @Bean
    public PaginationInterceptor paginationInterceptor() {

        return new PaginationInterceptor();
    }

    //自动填充的组件
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MetaObjectHandler() {//也可以自己创建一个类继承MetaObjectHandler
            @Override
            public void insertFill(MetaObject metaObject) {
                this.setFieldValByName("createTime", new Date(), metaObject);
                this.setFieldValByName("updateTime", new Date(), metaObject);
            }

            @Override
            public void updateFill(MetaObject metaObject) {

                this.setFieldValByName("updateTime", new Date(), metaObject);
                this.setFieldValByName("submitTime", new Date(), metaObject);
            }
        };
    }

}